import { _decorator, Component, EditBox, log, Node } from 'cc';
import { UIManager } from '../../framework/manager/uiManager';
import { Constant } from '../../Enum/Enum';
import { AccountService } from '../../gameCore/logic/service/AccountService';
import { proto } from '../../../../tools/proto/protos';
import { Util } from '../../framework/utils/util';
import ClientContext from '../../gameCore/global/ClientContext';
import { UserInfo } from '../../gameCore/vo/GOBERTS';
import ClientManager from '../../gameCore/global/ClientManager';
const { ccclass, property } = _decorator;
/**
 * 玩家注册登陆面板
 */
@ccclass('LoginPanel')
export class LoginPanel extends Component {
    @property(Node)
    loginPanel: Node = null!;
    @property(Node)
    hideAllButton: Node = null!;
    @property(Node)
    registButton: Node = null!;// 注册功能按钮
    @property(Node)
    loginButton: Node = null!;// 开始游戏功能按钮
    @property(EditBox)
    public accountBox: EditBox = null!;
    @property(EditBox)
    public pwdBox: EditBox = null!;

    async show() {
        this.loginPanel.active = true;
    }

    async hide() {// 关闭界面按钮点击事件
        UIManager.instance.hideDialog(Constant.PANEL_NAME.LOGIN_PANEL);
    }

    async registBtn() {// 登陆面板用户注册按钮触发
        console.log("触发注册按钮功能");
        const account = this.accountBox.textLabel.string;
        const pwd = this.pwdBox.textLabel.string;
        UIManager.instance.showDialog(Constant.PANEL_NAME.LOADING01);
        const flag = this.validateAccountAndPassword();
        if (!flag) {
            await Util.wait(Constant.WaitConfig.loadind);
            UIManager.instance.hideDialog(Constant.PANEL_NAME.LOADING01);
            console.log("账号或密码不规范");
            return;
        }
        console.log(account, pwd);
        const res: proto.UserInfoProto = await AccountService.Instance.register(account, pwd);// true 注册成功
        await Util.wait(Constant.WaitConfig.loadind);
        if (res) {
            ClientContext.Instance.UserInfo = res;// 更新客户端本地用户数据
            // await ContextClient.Instance.initUserState();// 不做了
            UIManager.instance.showTips(Constant.Tips.TIPS_1, null, Constant.TIPS_ANI_NAME.tips2aniTop);
            UIManager.instance.hideDialog(Constant.PANEL_NAME.LOGIN_PANEL);
        }
        UIManager.instance.hideDialog(Constant.PANEL_NAME.LOADING01);

    }
    /** 用户登陆出发按钮功能 */
    async loginBtn() {
        console.log("触发登陆按钮功能");
        UIManager.instance.showDialog(Constant.PANEL_NAME.LOADING01);
        const flag = this.validateAccountAndPassword();
        if (!flag) {
            await Util.wait(Constant.WaitConfig.loadind);
            UIManager.instance.hideDialog(Constant.PANEL_NAME.LOADING01);
            console.log("账号或密码不规范");
            return;
        }
        const ins = ClientContext.Instance;
        if (!ins.hasUserInfo()) {// 从服务器获取userInfo
            const userInfo = await AccountService.Instance.getUserInfo(this.accountBox.textLabel.string, this.pwdBox.textLabel.string);
            ClientContext.Instance.UserInfo = userInfo;
        }

        const res = ins.checkEnterGame(false);
        if (res == 0) {
            ClientManager.Instance.loadingHall();
        } else {
            UIManager.instance.showTips(res, null, Constant.TIPS_ANI_NAME.commonTipsAniCenter);
        }
        this.hide();
        UIManager.instance.hideDialog(Constant.PANEL_NAME.LOADING01);
    }
    /** 验证账号密码规范 */
    validateAccountAndPassword() {
        let account = this.accountBox.textLabel.string;
        let pwd = this.pwdBox.textLabel.string;
        if (account.length < 6 || account.length > 12 || !/^[a-zA-Z0-9]+$/.test(account)) {
            UIManager.instance.showTips(Constant.Tips.TIPS_2, null, Constant.TIPS_ANI_NAME.commonTipsAniCenter);
            return false;
        }
        if (pwd.length < 6 || pwd.length > 12 || !/^[a-zA-Z0-9]+$/.test(pwd)) {
            UIManager.instance.showTips(Constant.Tips.TIPS_3, null, Constant.TIPS_ANI_NAME.commonTipsAniCenter);
            return false;
        }
        return true;
    }
}

